package zkk.com.mengqu.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import zkk.com.mengqu.R;
import zkk.com.mengqu.manager.APIManager;
import zkk.com.mengqu.models.Address;
import zkk.com.mengqu.util.FinalString;

/**
 * Created by 40955 on 2017/8/21.
 */

public class AddressDetailActivity extends Activity implements View.OnClickListener{
    private Context context;
    private ImageView back;
    private TextView title, submit;
    private EditText etName, etPhone, etSex, etAddress, etAddressTag;

    private int type;
    private int addressId;
    private int addressUid;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_details);
        //默认不弹出软键盘
        getWindow().setSoftInputMode(   WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        type = getIntent().getIntExtra(FinalString.ADDRESS_DETAIL_TYPE, -1);
        addressId = getIntent().getIntExtra(FinalString.ADDRESS_ID, -1);
        addressUid = getIntent().getIntExtra(FinalString.ADDRESS_UID, -1);
        initView();
    }

    public void initView(){

        context = getApplicationContext();
        back = (ImageView)findViewById(R.id.iv_back);

        title = (TextView)findViewById(R.id.tx_title);
        submit = (TextView)findViewById(R.id.tx_address_submit);

        etName = (EditText)findViewById(R.id.et_name);
        etPhone = (EditText)findViewById(R.id.et_phone);
        etSex = (EditText)findViewById(R.id.et_sex);
        etAddress = (EditText)findViewById(R.id.et_address);
        etAddressTag = (EditText)findViewById(R.id.et_address_tag);

        title.setText(type == 1 ? "新增地址" : "修改地址");

        back.setOnClickListener(this);
        submit.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddress();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tx_address_submit:
                updateAddress();
                break;
        }
    }

    public void getAddress(){
        APIManager.setManager()
                .getAddress(addressUid, addressId)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Address>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.w("onError", "onError");
                    }

                    @Override
                    public void onNext(Address address) {
                        etName.setText(address.getName());
                        etPhone.setText(address.getPhone());
                        etSex.setText("" + address.getSex());
                        etAddress.setText(address.getAddress());
                        etAddressTag.setText(address.getAddressTag());
                    }
                });
    }

    public void updateAddress(){
        String name = etName.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        int sex = Integer.parseInt(etSex.getText().toString().trim());
        String addressStr = etAddress.getText().toString().trim();
        String addressTag = etAddressTag.getText().toString().trim();
        //暂定修改时全部变成不是默认地址
        boolean isDefault = false;
        Address address = new Address(addressId, addressUid, name, phone, sex, addressStr, addressTag, isDefault);
        APIManager.setManager()
                .updateAddress(address)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Address>() {
                    @Override
                    public void onCompleted() {
                        finish();
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        Log.w("onError", "onError");
                    }

                    @Override
                    public void onNext(Address address) {

                    }
                });
    }
}
